package com.wwp.tbshop.activity.cartactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.wwp.tbshop.R;
import com.wwp.tbshop.model.bean.InvoiceInfoBean;
import com.wwp.tbshop.presenter.InvoiceActivityPresenter;
import com.wwp.tbshop.util.Constant;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//import com.wwp.tbshop.R;


/**
 * Created by windows on 2017/6/15.
 */

public class InvoiceActivity extends Activity {


    @Bind(R.id.tv_jisuanzhongxin)
    TextView mTvJisuanzhongxin;
    @Bind(R.id.iv_geren)
    RadioButton mIvGeren;
    @Bind(R.id.iv_danwei)
    RadioButton mIvDanwei;
    @Bind(R.id.invoice_head)
    RadioGroup mInvoiceHead;
    @Bind(R.id.rb_one)
    RadioButton mRbOne;
    @Bind(R.id.rb_two)
    RadioButton mRbTwo;
    @Bind(R.id.rb_three)
    RadioButton mRbThree;
    @Bind(R.id.rb_four)
    RadioButton mRbFour;
    @Bind(R.id.invoice_info)
    RadioGroup mInvoiceInfo;
    @Bind(R.id.btn_commited)
    Button mBtnCommited;
    private InvoiceActivityPresenter mInvoiceActivityPresenter;
    private int type = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);
        ButterKnife.bind(this);

        mInvoiceActivityPresenter = new InvoiceActivityPresenter(this);
        mInvoiceHead.check(R.id.iv_geren);
        mInvoiceInfo.check(R.id.rb_one);
        setOnListener();
    }

    private void setOnListener() {
      /*  mInvoiceHead.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.iv_geren) {
                    mIvGeren.setVisibility(View.VISIBLE);
                } else {
                    mIvDanwei.setVisibility(View.GONE);
                }
                if (checkedId == R.id.iv_danwei){
                    mIvDanwei.setVisibility(View.VISIBLE);
                }else {
                    mIvGeren.setVisibility(View.GONE);
                }
            }
        });*/

        mBtnCommited.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void submit() {
        String head = getHead();
        String info = getInfo();
        Intent data = new Intent();
        data.putExtra("invoiceHead", head);
        data.putExtra("invoiceInfo", info);
        data.putExtra("invoiceId", type);
        setResult(Constant.INVOICE_REQUEST_CODE, data);
        finish();
    }

    private String getHead() {
        String invoiceHead;
        int headId = mInvoiceHead.getCheckedRadioButtonId();
        if (headId == R.id.iv_danwei) {

            invoiceHead = "单位";
        } else {
            invoiceHead = "个人";
        }
        return invoiceHead;
    }

    private String getInfo() {
        int infoId = mInvoiceInfo.getCheckedRadioButtonId();
        switch (infoId) {
            case R.id.rb_one:
                type = 1;
                break;
            case R.id.rb_two:
                type = 2;
                break;
            case R.id.rb_three:
                type = 3;
                break;
            case R.id.rb_four:
                type = 4;
                break;
        }
        String text = ((RadioButton) findViewById(infoId)).getText().toString();
        return text;
    }


    @OnClick(R.id.tv_jisuanzhongxin)
    public void onClick() {
        finish();
    }

    public void getDataSuccess(InvoiceInfoBean bean) {
        List<InvoiceInfoBean.InvoiceBean> invoice = bean.getInvoice();
        mRbOne.setText(invoice.get(0).getContent());
        mRbTwo.setText(invoice.get(1).getContent());
        mRbThree.setText(invoice.get(2).getContent());
        mRbFour.setText(invoice.get(3).getContent());
    }
}
